package com.iuv.shortmsg.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * 短信工具类
 *
 * @author Will
 * @date 17:04
 */
public class SmsUtil {
    /**
     * 产品名称:云通信短信API产品,开发者无需替换
     */
    static final String product = "Dysmsapi";
    /**
     * 产品域名,开发者无需替换
     */
    static final String domain = "dysmsapi.aliyuncs.com";

    /**
     * 用户AccessKey
     */
    static final String accessKeyId = "LTAI4GEUrnLSZgRPjCcfFSik";
    /**
     * AccessKey密钥
     */
    static final String accessKeySecret = "jr97i2IMItJUpbzjEu2PoEMXFkhXoY";

    /**
     * 短信发送
     *
     * @param phoneNumber   手机号码
     * @param signName      短信签名
     * @param templateCode  短信模板
     * @param templateParam 模板参数(Json字符串)
     * @return
     */
    public static SendSmsResponse sendSms(String phoneNumber, String signName, String templateCode, String templateParam) {
        //超时时间设置
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        SendSmsResponse sendSmsResponse = null;
        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        //待发送手机号码，必填项
        sendSmsRequest.setPhoneNumbers(phoneNumber);
        //短信签名，必填项
        sendSmsRequest.setSignName(signName);
        //短信模板，必填项
        sendSmsRequest.setTemplateCode(templateCode);
        //模板参数，非必填项（当短信模板没有定义参数时无需填入）
        if (templateParam != null) {
            sendSmsRequest.setTemplateParam(templateParam);
        }
        try {
            //发送短信
            sendSmsResponse = acsClient.getAcsResponse(sendSmsRequest);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return sendSmsResponse;
    }
}
